package com.northwest_man.refactor_tool.statistics;

import com.northwest_man.refactor_tool.FileUtil;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * 收集到的 event_id 去重
 */
public class StatisticsDeDuplication {
    private static final String originalFilePath = "/Users/yong.hu/Desktop/PatPat_Statistics_exposure.txt";
    private static final String newFilePath = "/Users/yong.hu/Desktop/newPatPat_Statistics_exposure.txt";

    private static final StringBuilder fileContent = FileUtil.getFileContent(originalFilePath);

    public static void main(String[] args) {
        final String[] originalEventIdArray = fileContent.toString().split("\n");
        // 第一步先去掉格式(正则表达式)一致的 event_id，相同格式的 event_id 只留下一个[开始]
        final StatisticsRegular[] regexArray = StatisticsRegular.values();
        final StringBuilder newEventIdStringBuilder = new StringBuilder();
        final Map<StatisticsRegular, Boolean> existMap = new HashMap<>();
        for (final String eventId : originalEventIdArray) {
            boolean exist = false;
            for (final StatisticsRegular regex : regexArray) {
                if (eventId.matches(regex.regex)) {
                    if (existMap.get(regex) != null) {
                        exist = true;
                    } else {
                        existMap.put(regex, true);
                    }

                    break;
                }
            }
            if (!exist) {
                newEventIdStringBuilder.append(eventId).append("\n");
            }
        }
        // 第一步先去掉格式(正则表达式)一致的 event_id，相同格式的 event_id 只留下一个[结束]
        try {
            FileUtils.writeStringToFile(new File(newFilePath), newEventIdStringBuilder.toString(), Charset.defaultCharset());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
